import ToolBar from '@/components/ToolBar'
import { getList, deleteItem, activateDevice } from '@/api/terminal'

const operations = {
  deleteItem,
  activateDevice
}

export default {
  components: {
    ToolBar
  },
  data() {
    return {
      tableData: [],
      listLoading: false,
      selectedData: [],
      pageNo: 1,
      pageSize: 10,
      total: 0,
    }
  },
  methods: {
    statusFilter(status) {
      const statusMap = {
        0: 'Locked',
        1: 'Activated',
        3: 'Under Repair',
        4: 'Lost',
        5: 'Unactivated',
        7: 'Transfer',
        8: 'Inventory',
        9: 'Scrapped',
      }
      return statusMap[status]
    },
    fetchData(params) {
      this.listLoading = true
      getList(params).then(res => {
        this.listLoading = false
        this.tableData = res.data
        this.total = res.rows
      })
    },

    handleSelectionChange(val) {
      this.selectedData = val
    },

    handleActive() {},
    operate(operation) {
      let opType = 'Active'
      if (operation === 'deleteItem') {
        opType = 'Delete'
      }

      this.$confirm(`Are you sure to ${opType} this selection items?`,
        this.$t('common.tips'),
        {
          confirmButtonText: this.$t('page.confirm'),
          cancelButtonText: this.$t('page.cancel'),
          type: 'warning'
        }).then(
        () => {
          operations[operation]({
            devSnList: this.selectedData.map((data) => data.devSn)
          }).then(() => {
            this.search()
            this.$message.success('Operated Successfully.')
          })
        }
      )
    },
  }
}
